package com.zunipe.until;

import com.zunipe.screen.OptionScreen;
import com.zunipe.screen.ScanningBoxOptionScreen;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.GenericContainerScreen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.inventory.Inventory;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.text.LiteralText;
import net.minecraft.util.crash.CrashException;

import java.util.Iterator;

public class ScanningBoxFunction
{
    public static void ScanningBox(MinecraftClient client)
    {
        if (client.player != null)
        {
            if (!Util.scanningBox)
            {
                Util.scanningBox = true;
                new Thread(() ->
                {
                    client.player.sendMessage(new LiteralText("§l垃圾桶扫描者 §2§l开启"), true);
                    try
                    {
                        client.player.sendChatMessage("/bs");
                        Thread.sleep(500);
                        if (client.currentScreen instanceof HandledScreen<?>)
                            client.interactionManager.clickSlot(((HandledScreen<?>) client.currentScreen).getScreenHandler().syncId, 36 + 4, 1, SlotActionType.PICKUP, client.player);
                        Thread.sleep(1000);
                    } catch (CrashException | InterruptedException ignored)
                    {
                        client.player.sendMessage(new LiteralText("§l垃圾桶扫描者 §4§l关闭"), true);
                        Util.scanningBox = false;
                    }
                    while (Util.scanningBox)
                    {
                        Screen screen = client.currentScreen;
                        if (screen == null)
                        {
                            client.player.sendMessage(new LiteralText("§l垃圾桶扫描者 §4§l关闭"), true);
                            Util.scanningBox = false;
                            break;
                        }
                        if (screen instanceof GenericContainerScreen)
                        {
                            Inventory inventory = ((GenericContainerScreen) screen).getScreenHandler().getInventory();
                            for (int i = 0; i < inventory.size() - 9; ++i)
                            {
                                boolean flag = false;
                                for (Iterator<String> it = Util.itemList.stream().iterator(); it.hasNext(); )
                                {
                                    String name = it.next();
                                    if (inventory.getStack(i).getName().getString().contains(name))
                                    {
                                        flag = true;
                                        for (Iterator<String> it2 = Util.exclusionItemList.stream().iterator(); it2.hasNext(); )
                                        {
                                            String name2 = it2.next();
                                            if (inventory.getStack(i).getName().getString().contains(name2))
                                            {
                                                flag = false;
                                                break;
                                            }
                                        }
                                        break;
                                    }
                                }
                                if (flag)
                                {
                                    try
                                    {
                                        client.interactionManager.clickSlot(((GenericContainerScreen) screen).getScreenHandler().syncId, i, 0, SlotActionType.PICKUP, client.player);
                                        Thread.sleep(100);
                                    } catch (CrashException | InterruptedException crashException)
                                    {
                                        client.player.sendMessage(new LiteralText("§l垃圾桶扫描者 §4§l关闭"), true);
                                        Util.scanningBox = false;
                                    }
                                }
                            }
                            try
                            {
                                client.interactionManager.clickSlot(((GenericContainerScreen) screen).getScreenHandler().syncId, Util.scanningMode.equals(ScanningBoxOptionScreen.ScanningMode.BACKWARD) ? inventory.size() - 1 : inventory.size() - 9, 0, SlotActionType.PICKUP, client.player);
                                Thread.sleep(100);
                            } catch (CrashException | InterruptedException crashException)
                            {
                                client.player.sendMessage(new LiteralText("§l垃圾桶扫描者 §4§l关闭"), true);
                                Util.scanningBox = false;
                            }
                        }
                    }
                }).start();
            }
            else
            {
                client.player.sendMessage(new LiteralText("§l垃圾桶扫描者 §4§l关闭"), true);
                Util.scanningBox = false;
            }
        }
    }

}
